﻿using System;
using System.Data.SqlTypes;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDT.Libraries.Utilities
{
    public class ConvertHelper
    {
        public static Guid ToGuid(object val, Guid defValue)
        {
            Guid ret = defValue;
            try
            {
                ret = new Guid(val.ToString());
            }
            catch { }

            return ret;
        }
        public static Guid ToGuid(object val)
        {
            return ToGuid(val.ToString(), Guid.Empty);
        }

        public static Guid ToGuid(SqlGuid val)
        {
            return val.IsNull ? Guid.Empty : val.Value;
        }

        public static int ToInt32(object obj)
        {
            int retVal = 0;

            try
            {
                retVal = Convert.ToInt32(obj);
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }

        public static int ToInt32(object obj, int defaultValue)
        {
            int retVal = defaultValue;

            try
            {
                if (obj == null)
                    return defaultValue;
                retVal = Convert.ToInt32(obj);
            }
            catch
            {
                retVal = defaultValue;
            }

            return retVal;
        }

        public static string ToString(object obj)
        {
            string retVal;

            try
            {
                retVal = Convert.ToString(obj);
            }
            catch
            {
                retVal = String.Empty;
            }

            return retVal;
        }

        public static DateTime ToDateTime(object obj)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = DateTime.Now;
            }
            if (retVal == new DateTime(1, 1, 1)) return DateTime.Now;

            return retVal;
        }

        public static DateTime ToDateTime(object obj, DateTime defaultValue)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = DateTime.Now;
            }
            if (retVal == new DateTime(1, 1, 1)) return defaultValue;

            return retVal;
        }
        public static bool ToBoolean(object obj)
        {
            bool retVal;

            try
            {
                retVal = Convert.ToBoolean(obj);
            }
            catch
            {
                retVal = false;
            }

            return retVal;
        }

        public static double ToDouble(object obj)
        {
            double retVal;

            try
            {
                retVal = Convert.ToDouble(obj);
            }
            catch
            {
                retVal = 0;
            }

            return retVal;
        }

        public static double ToDouble(object obj, double defaultValue)
        {
            double retVal;

            try
            {
                retVal = Convert.ToDouble(obj);
            }
            catch
            {
                retVal = defaultValue;
            }

            return retVal;
        }

        public static Decimal ToDecimal(object val, Decimal defValue)
        {
            Decimal ret = defValue;
            try
            {
                ret = Convert.ToDecimal(val);
            }
            catch { }

            return ret;
        }

        public static Decimal ToDecimal(object val)
        {
            return ConvertHelper.ToDecimal(val, 0);
        }

        public static Decimal ToDecimal(SqlDecimal val)
        {
            return val.IsNull ? Decimal.Zero : val.Value;
        }

        public const string VIETNAMESE = "VN";
        public const string ENGLISH = "US";	

        public static string DisplayMoney(Decimal fvalue, String strCountry)
        {
            string strValue = fvalue.ToString(".##");
            string strTail = String.Empty;
            string strHead = strValue;

            int nLength = strValue.Length;

            if (strCountry == VIETNAMESE)
            {
                if (strValue.LastIndexOf(".") != -1)
                {
                    strHead = strValue.Substring(0, strValue.LastIndexOf("."));
                    strTail = strValue.Substring(strValue.LastIndexOf("."));
                    strTail = strTail.Replace(",", ".");
                    nLength = strHead.Length;
                }

                string strRet = String.Empty;
                Stack st = new Stack();
                while (nLength > 3)
                {
                    st.Push(strHead.Substring(nLength - 3));
                    strHead = strHead.Substring(0, nLength - 3);
                    nLength = strHead.Length;
                }

                while (st.Count != 0)
                {
                    strRet += "," + st.Pop();
                }
                return (strHead + strRet + strTail);
                
            }
            else
            //if (strCountry == ENGLISH)
            {
                if (strValue.LastIndexOf(".") != -1)
                {
                    strHead = strValue.Substring(0, strValue.LastIndexOf("."));
                    strTail = strValue.Substring(strValue.LastIndexOf("."));
                    nLength = strHead.Length;
                }

                string strRet = String.Empty;
                Stack st = new Stack();
                while (nLength > 3)
                {
                    st.Push(strHead.Substring(nLength - 3));
                    strHead = strHead.Substring(0, nLength - 3);
                    nLength = strHead.Length;
                }

                while (st.Count != 0)
                {
                    strRet += "," + st.Pop();
                }

                if (strTail == String.Empty)
                {
                    strTail = ".00";
                }
                return ("$ " + strHead + strRet + strTail);
            }

            

        }

    }
}